package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.bo.SpuBo;
import com.bwie.mapper.*;
import com.bwie.pojo.*;
import com.bwie.service.SpuService;
import com.bwie.utils.PageResult;
import com.bwie.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

/**
 * @program: day0423-pxx
 * @ClassName SpuServiceImpl
 * @description:
 * @author: 段阳
 * @create: 2024-04-29 21:33
 **/
@Service
@Transactional
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Override
    public ResponseEntity<PageResult<SpuBo>> findSpuList(String key, Integer page, Integer rows, Boolean saleable) {
        Page<Spu> spuPage = new Page<>(page,rows);
        QueryWrapper<Spu> spuQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(key)){
            spuQueryWrapper.lambda().like(Spu::getTitle,key);
        }
        if (saleable!=null){
            spuQueryWrapper.lambda().eq(Spu::getSaleable,saleable);
        }
        Page<Spu> spuPage1 = spuMapper.selectPage(spuPage, spuQueryWrapper);
        PageResult<SpuBo> spuBoPageResult = new PageResult<>();
        spuBoPageResult.setTotal(spuPage1.getTotal());
        spuBoPageResult.setPages(spuPage1.getPages());
        List<Spu> records = spuPage1.getRecords();
        ArrayList<SpuBo> spuBos = new ArrayList<>();
        for (Spu record : records) {
            SpuBo spuBo = BeanUtil.toBean(record, SpuBo.class);
            String categoryName = "";
            Long cid1 = spuBo.getCid1();
            Category category = categoryMapper.selectById(cid1);
            categoryName = category.getName();
            Long cid2 = spuBo.getCid2();
            Category category2 = categoryMapper.selectById(cid2);
            categoryName += "/"+ category2.getName();
            Long cid3 = spuBo.getCid3();
            Category category3 = categoryMapper.selectById(cid3);
            categoryName += "/"+ category3.getName();
            spuBo.setCname(categoryName);
            Brand brand = brandMapper.selectById(spuBo.getBrandId());
            spuBo.setBname(brand.getName());
            spuBos.add(spuBo);
        }
        spuBoPageResult.setItems(spuBos);
        return ResponseEntity.ok(spuBoPageResult);
    }

    @Override
    public R addSpuInfo(SpuBo spuBo) {
        Spu spu = BeanUtil.toBean(spuBo, Spu.class);
        spu.setCreateTime(new Date());
        spu.setLastUpdateTime(new Date());
        spuMapper.insert(spu);
        Long id = spu.getId();
        SpuDetail spuDetail = BeanUtil.toBean(spuBo.getSpuDetail(), SpuDetail.class);
        spuDetail.setSpuId(id);
        spuDetailMapper.insert(spuDetail);
        addNewSku(id,spuBo);
        return R.success("success");
    }

    @Override
    public R upsaleable(Long id) {
        Spu spu = spuMapper.selectById(id);
        UpdateWrapper<Spu> spuUpdateWrapper = new UpdateWrapper<>();
        if (spu.getSaleable()){
            spuUpdateWrapper.lambda().set(Spu::getSaleable,false).eq(Spu::getId,spu.getId());
        }else {
            spuUpdateWrapper.lambda().set(Spu::getSaleable,true).eq(Spu::getId,spu.getId());
        }
        spuMapper.update(spu,spuUpdateWrapper);
        return R.success("success");
    }

    @Override
    public R delSpu(Long id) {
        List<Sku> skus = skuMapper.selectList(new QueryWrapper<Sku>().lambda().eq(Sku::getSpuId, id));
        for (Sku sku : skus) {
            stockMapper.deleteById(sku.getId());
        }
        skuMapper.delete(new QueryWrapper<Sku>().lambda().eq(Sku::getSpuId,id));
        spuDetailMapper.deleteById(id);
        spuMapper.deleteById(id);
        return R.success("success");
    }

    @Override
    public SpuDetail getSpuDetail(Long spuId) {
        SpuDetail spuDetail = spuDetailMapper.selectById(spuId);
        return spuDetail;
    }

    @Override
    public R upSpuInfo(SpuBo spuBo) {
        List<Sku> list = skuMapper.selectList(new QueryWrapper<Sku>().lambda().eq(Sku::getSpuId, spuBo.getId()));
        if (!CollectionUtil.isEmpty(list)){
            for (Sku sku : list) {
                stockMapper.deleteById(sku.getId());
                skuMapper.deleteById(sku.getId());
            }
        }
        Spu spu = BeanUtil.toBean(spuBo, Spu.class);
        spu.setLastUpdateTime(new Date());
        spuMapper.updateById(spu);
        Long id = spu.getId();
        SpuDetail spuDetail = BeanUtil.toBean(spuBo.getSpuDetail(), SpuDetail.class);
        spuDetailMapper.updateById(spuDetail);
        addNewSku(id,spuBo);
        return R.success("success");
    }
    /**
    * //TODO
    * @author 段阳
    * @date 2024/5/6 15:31
     * @param spuBo
    */
    public void addNewSku(Long id,SpuBo spuBo){
        List<Sku> skus = spuBo.getSkus();
        for (Sku sku : skus) {
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(new Date());
            sku.setSpuId(id);
            skuMapper.insert(sku);
            Long id1 = sku.getId();
            Integer stock = sku.getStock();
            stockMapper.insert(new Stock(id1,null,null,stock));
        }
    }
}